/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.consultar;

import br.com.deployment.entity.orcamento.Orcamento;
import br.com.deployment.entity.orcamento.OrcamentoAto;
import br.com.deployment.servico.OrcamentoAtoServico;
import br.com.deployment.servico.OrcamentoServico;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerConsultarOrcamento implements Serializable{
    
    @EJB
    OrcamentoServico orcamentoService;
    
    @EJB
    OrcamentoAtoServico orcamentoAtoServico;
    
    private List<Orcamento> lstOrcamento;
    private Orcamento orcamento;
    private Orcamento orcamentoVisualizar;
    private Integer cod;


    
    @PostConstruct
    public void init(){
        orcamento = new Orcamento();
        orcamentoVisualizar = new Orcamento();
        lstOrcamento = new ArrayList<Orcamento>();
    }
    
    public void reinit(){
        orcamento = new Orcamento();
        orcamentoVisualizar = new Orcamento();
        lstOrcamento = new ArrayList<Orcamento>();
    }

    public Integer getCod() {
        return cod;
    }

    public void setCod(Integer cod) {
        this.cod = cod;
    }
    
    

    public Orcamento getOrcamento() {
        return orcamento;
    }

    public void setOrcamento(Orcamento orcamento) {
        this.orcamento = orcamento;
    }

    
    public List<Orcamento> getLstOrcamento() {
        return lstOrcamento;
    }

    public void setLstOrcamento(List<Orcamento> lstOrcamento) {
        this.lstOrcamento = lstOrcamento;
    }

    public Orcamento getOrcamentoVisualizar() {
        return orcamentoVisualizar;
    }

    public void setOrcamentoVisualizar(Orcamento orcamentoVisualizar) {
        this.orcamentoVisualizar = orcamentoVisualizar;
    }
    
    
    
    public void buscar(){
        if(cod != null){
            orcamento.setId(cod);
        }
        lstOrcamento = orcamentoService.pesquisar(orcamento, cod);
//        orcamento = new Orcamento();
        System.err.println(orcamento.getId());
    }
    
    public void dialogShow(){
        orcamentoVisualizar = new Orcamento();
        
        FacesContext context = FacesContext.getCurrentInstance();
        Map map = context.getExternalContext().getRequestParameterMap();
        String str = (String) map.get("paramShowOrcAtoPesquisar");
        int ds = Integer.parseInt(str);
        
        orcamentoVisualizar = (lstOrcamento.get(ds));
        orcamentoVisualizar.setOrcamentoAtos(orcamentoAtoServico.getOrcamento(orcamentoVisualizar));
    }
    
    public String somaServicos(){
        BigDecimal sum = BigDecimal.ZERO;
        String aux = "0";
        if(orcamentoVisualizar.getOrcamentoAtos() != null){
            orcamentoVisualizar.setOrcamentoAtos(orcamentoAtoServico.getOrcamento(orcamentoVisualizar));
            for (OrcamentoAto oa : orcamentoVisualizar.getOrcamentoAtos()) {
                sum = sum.add(oa.getValor());
            }
            
        }
        DecimalFormat df = new DecimalFormat("#,##0.00");
        aux = String.valueOf(df.format(sum));
        return aux;
    }
    
    
    
      
    
}
